package com.welsy.stats.command
{
	import com.welsy.stats.model.StatsModelLocator;
	import com.welsy.stats.vo.IterClassVO;
	import com.adobe.cairngorm.commands.ICommand;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.stats.business.GetClassroomTrackingStatsDelegate;
	import mx.rpc.events.ResultEvent;
	import com.welsy.stats.vo.IterElementStatsVO;
	import com.welsy.stats.view.classview.CourseView;
	import com.welsy.stats.view.classview.CourseView;
	import com.welsy.stats.view.classview.IterView;
	import com.welsy.stats.vo.IterStatsVO;
	import com.welsy.stats.view.classview.IterView;
	import com.welsy.stats.view.classview.IterView;
	import com.welsy.stats.event.GetClassroomCourseStatsEvent;
	import com.welsy.stats.vo.TestVO;
	import com.welsy.stats.view.classview.TestView;
	import com.welsy.stats.view.classview.TestView;
	import com.welsy.stats.business.GetClassroomCourseStatsDelegate;
	
	public class GetClassroomCourseStatsCommand implements ICommand, IResponder
	{
		[Bindable] private var smodel : StatsModelLocator = StatsModelLocator.getInstance();	
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetClassroomCourseStatsDelegate = new GetClassroomCourseStatsDelegate(this);
			var gccs_eve : GetClassroomCourseStatsEvent = GetClassroomCourseStatsEvent(event);
			
			delegate.getClassroomCourseStats(gccs_eve.classroomId, gccs_eve.iterElementId, gccs_eve.courseId);
		}
		
		public function result(event : Object) :void {
			//parse result
			var xres : XMLList = XMLList(ResultEvent(event).result);
			
			var arNS : Namespace = new Namespace(NamespaceLocator.ADMINRESULTS_DATATYPES);
			
			var TAG_GETCLASSROOMTRACKINGSTATS : QName = new QName(arNS, "GetClassroomCourseStatsResult");
			var TAG_COURSEID : QName = new QName(arNS, "CourseId");
			var TAG_TESTSTATSCOLLECTION : QName = new QName(arNS, "TestStatsCollection");
			
			var TAG_TESTID : QName = new QName(arNS, "TestId");
			var TAG_TITLE : QName = new QName(arNS, "Title");
			var TAG_PASSED : QName = new QName(arNS, "Passed");
			var TAG_TOTAL : QName = new QName(arNS, "Total");
			
			var gcts_e : XMLList = xres.descendants(TAG_GETCLASSROOMTRACKINGSTATS);
			var ci_e : String = gcts_e.child(TAG_COURSEID);
			
			var tsc_e : XMLList = gcts_e.descendants(TAG_TESTSTATSCOLLECTION).children();//TestStatsCollection
			smodel.currentCourse.tests = new ArrayCollection;
			for each(var tst_e : XML in tsc_e) {
				//TestStatsType
				var testVO : TestVO = new TestVO;
				var testView : TestView = new TestView;
				testVO.testId = tst_e.child(TAG_TESTID);
				testVO.title = tst_e.child(TAG_TITLE);
				testVO.passed = tst_e.child(TAG_PASSED);
				testVO.total = tst_e.child(TAG_TOTAL);
				testView.testVO = testVO;
				
				smodel.currentCourse.tests.addItem(testView);
			}
			//refresh the graphical representation
			smodel.currentCourse.refreshChildren(null);
		}
		
		public function fault( event : Object ):void {
			Alert.show("GetClassroomCourseStatsCommand.fault!");
		}
		
	}
}